//
//  RateQueryCell.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/12.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class RateQueryCell: UITableViewCell {
    
    @IBOutlet weak var cardView: UIView!
    
    @IBOutlet weak var flagImageView: UIImageView!
    
    @IBOutlet weak var currencyView: UIView!
    @IBOutlet weak var currencyLabel: UILabel!
    @IBOutlet weak var currencyTagLabel: UILabel!
    
    @IBOutlet weak var inputTF: UITextField!
    
    private let notificationName = NSNotification.Name("changeRateValueNotif")
    
    var changeRateAction:((Rate?)->Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.backgroundColor = kThemeBgColor
        self.cardView.addShadow(type: .dayArrange)
        self.currencyView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(changeRate)))
        
        NotificationCenter.default.addObserver(self, selector: #selector(reloadRealMoneyNotif(notif:)), name: notificationName, object: nil)
        
        inputTF.delegate = self
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc func changeRate() {
        
        self.changeRateAction?(self.rate)
    }
    
    var rate:Rate? {
        
        didSet {
            
            flagImageView.setImage(string: rate?.pictures, placeholder: UIImage(named:"placeholder_squ")!)
            currencyLabel.text = rate?.code
            currencyTagLabel.text = rate?.smartName
            
            reloadRealMoney()
        }
    }
    
    @objc func reloadRealMoneyNotif(notif:Notification) {
        
        guard let model = notif.userInfo?["model"] as? Rate,
            let rate = rate else {
            
            return
        }
        
        if let code1 = rate.code, let code2 = model.code, code1 == code2 {
            
            return
        }
        
        reloadRealMoney()
    }
    
    func reloadRealMoney() {
        
        guard let rate = rate else {
            
            return
        }
        
        let realMoney = RateManager.share.calcRealMoney(rate: rate)
        
        self.inputTF.placeholder = realMoney
        
        if let _ = RateManager.share.basemoney {
            
            self.inputTF.text = realMoney
        } else {
            
            self.inputTF.text = nil
        }
    }
}

extension RateQueryCell: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if let rate = rate,
            let text = textField.text,
            let textRange = Range(range, in: text) {
            
            let updatedText = text.replacingCharacters(in: textRange,with: string)
            
            RateManager.share.calcBaseMoney(rate: rate, money: updatedText)
            
            NotificationCenter.default.post(name:notificationName , object: nil, userInfo: ["model":rate])
        }
        return true
    }
}



